﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace TRIR
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hideObrazce();
            

            if (Page.IsPostBack)
            {

                showObrazce();

                if (box.Text != "")
                {

                    if (box.Text != null)
                    {
                        //youtube
                        Feed<Video> video = prikaziYoutube(box.Text);

                        //izpis youtube
                        foreach (Video entry in video.Entries)
                        {
                            youtube.Text = izpisiYoutube(entry.VideoId);
                        }

                        //imdb podatki

                        WebClient wc1 = new WebClient();

                        string imdbXML = wc1.DownloadString("http://www.imdbapi.com/?t="+ box.Text.ToLower() +"&r=XML&plot=full");

                        //parsanje xml
                        XmlDocument doc1 = new XmlDocument();
                        doc1.InnerXml = imdbXML;
                        XmlElement root1 = doc1.DocumentElement;
                        XmlNodeList movieIMDB = doc1.GetElementsByTagName("movie");

                        string igralci = "";

                        if (movieIMDB.Count < 1)
                        {
                            naslovFilma.Text = "Ni podatkov o filmu (napačno vnesen film)";
                        }
                        else
                        {
                            for (int i = 0; i < movieIMDB.Count; i++)
                            {
                                naslovFilma.Text = movieIMDB[i].Attributes["title"].Value;
                                ocenaFilma.Text = movieIMDB[i].Attributes["imdbRating"].Value;
                                letoIzida.Text = movieIMDB[i].Attributes["year"].Value;
                                opis.Text = movieIMDB[i].Attributes["plot"].Value;
                                dolzina.Text = movieIMDB[i].Attributes["runtime"].Value;
                                kategorija.Text = movieIMDB[i].Attributes["genre"].Value;
                                slika.Text = "<img src='" + movieIMDB[i].Attributes["poster"].Value + " height='480' width='320'/>'";
                                direktorji.Text = movieIMDB[i].Attributes["director"].Value;
                                scenaristi.Text = movieIMDB[i].Attributes["writer"].Value;

                                //za prikaz informacij o igralcih
                                igralci = movieIMDB[i].Attributes["actors"].Value;
                            }

                            //igralci = igralci.Trim();
                            string[] igralciLista = igralci.Split(new string[] { ", " }, StringSplitOptions.None);
                            nastopajo.Text = "";

                            for (int i = 0; i < igralciLista.Length; i++)
                            {
                                nastopajo.Text += "<br /><a href='Igralec.aspx?igralec=" + igralciLista[i] + "'>" + igralciLista[i] + "</a>";
                            }

                        }

                        //checkbox za prikaz dodatnih informacij (kino)
                        if (prikaziFilmeIgralcev.Checked)
                        {
                            seznamPredvajanj.Text = "";

                            String kinodvorana = kinodvorane.SelectedValue;
                            kino.Text = kinodvorana;

                            //split stringa iz dropdownliste
                            String kinoS = kinodvorana.Split()[0];
                            String mestoS = kinodvorana.Split()[1];

                            //kliče spletno storitev
                            WebClient wc = new WebClient();

                            string xmlKino = wc.DownloadString("http://localhost:8080/linkedMDB/endpoint/movie/shows/" + mestoS+"-"+kinoS);

                            //parse xml
                            XmlDocument doc = new XmlDocument();
                            doc.InnerXml = xmlKino;
                            XmlElement root = doc.DocumentElement;
                            XmlNodeList movie = doc.GetElementsByTagName("titleEng");
                            XmlNodeList date = doc.GetElementsByTagName("date");
                            XmlNodeList time = doc.GetElementsByTagName("time");
                            XmlNodeList theater = doc.GetElementsByTagName("theater");

                            if (movie.Count < 1)
                            {
                                seznamPredvajanj.Text = "Ni podatkov o predvajanju tega filma";
                            }
                            else
                            {
                                for (int i = 0; i < movie.Count; i++)
                                {
                                    if(movie[i].InnerText.ToLower() == box.Text.ToLower()) {
                                        seznamPredvajanj.Text += "<br /><b>Datum: </b>" + date[i].InnerText + ", <b>ura: </b>" + time[i].InnerText + ", <b>lokacija: </b>" + theater[i].InnerText;
                                    }
                                }
                                if (seznamPredvajanj.Text == "")
                                {
                                    seznamPredvajanj.Text = "Ta film se trenutno ne predvaja v izbrani kinodvorani";
                                }
                            }
                        }
                        else 
                        {
                            seznamPredvajanj.Text = "Niste izbrali možnosti za prikaz predvajanj";
                        }

                    }
                    else
                    {
                        naslovFilma.Text = "Film s tem naslovom ne obstaja";
                        youtube.Text = "";
                    }
                }
                else
                {
                    naslovFilma.Text = "Niste vnesli filma!";
                    youtube.Text = "";
                }
            }
        }

        private void showObrazce()
        {
            zgoraj.Visible = true;
            spodaj.Visible = true;
        }

        private void hideObrazce()
        {
            zgoraj.Visible = false;
            spodaj.Visible = false;
        }



        protected Feed<Video> prikaziYoutube(string film)
        {
            //AI39si7fUFuHMTssYu-idFMmA9FhJcGBdZpAd3PUeSD2mYEbikIqe9CBzALjF70-X5Pp28r9EG2WznCZ0GvKUa4opFvNQt7TGA


            YouTubeRequestSettings settings = new YouTubeRequestSettings("tehnologijeRIR", "AI39si7fUFuHMTssYu-idFMmA9FhJcGBdZpAd3PUeSD2mYEbikIqe9CBzALjF70-X5Pp28r9EG2WznCZ0GvKUa4opFvNQt7TGA");
            
            YouTubeRequest request = new YouTubeRequest(settings);
            YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);

            //order results by the number of views (most viewed first)
            //query.OrderBy = "viewCount";

            // search for puppies and include restricted content in the search results
            // query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderate
            query.Query = film + " trailer";

            Feed<Video> videoFeed = request.Get<Video>(query);

            //printVideoFeed(videoFeed);

            return videoFeed;
        }

        protected string izpisiYoutube(string id)
        {
            string scr = @"<object width='800' height='600'> ";
            scr = scr + @"<param name='movie' value='http://www.youtube.com/v/" + id + "'></param> ";
            scr = scr + @"<param name='allowFullScreen' value='true'></param> ";
            scr = scr + @"<param name='allowscriptaccess' value='always'></param> ";
            scr = scr + @"<embed src='http://www.youtube.com/v/" + id + "' ";
            scr = scr + @"type='application/x-shockwave-flash' allowscriptaccess='always' ";
            scr = scr + @"allowfullscreen='true' width='320' height='240'> ";
            scr = scr + @"</embed></object>";
            return scr;
        }
        
    }
}
